package net.cloudcodex.client.sheet;

import java.util.HashMap;
import java.util.List;

import com.allen_sauer.gwt.dnd.client.DragEndEvent;
import com.allen_sauer.gwt.dnd.client.DragHandler;
import com.allen_sauer.gwt.dnd.client.DragStartEvent;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * 
 * @author ADaroussin
 */
public class NamedImageSheet extends FlowPanel implements Sheet, DragHandler {

	private final Label label;
	private final Image image;
	private final SheetLabel title;
	private final SheetImage preview;
	private final SheetLabel footer;
	private final List<Sheet> links;

	private final HashMap<ClickHandler, HandlerRegistration> handlers = new HashMap<ClickHandler, HandlerRegistration>();

	private int left;
	private int top;

	public NamedImageSheet(String labelText, String imageUrl, List<Sheet> links) {
		label = new Label(labelText);
		image = new Image(imageUrl);
		image.setTitle(labelText);

		preview = new SheetImage(imageUrl, "Preview of " + labelText, this);
		preview.setStylePrimaryName("ccSheetPreview");
		preview.addStyleName("transition");

		title = new SheetLabel(labelText, this);
		title.setStylePrimaryName("ccSheetTitle");
		title.addStyleName("transition");

		footer = new SheetLabel("Footer of " + labelText, this);
		footer.setStylePrimaryName("ccSheetFooter");
		footer.addStyleName("transition");

		this.links = links;

		this.setStylePrimaryName("ccSheet");
		this.add(label);
		this.add(image);
	}

	public Sheet asSheet() {
		return this;
	}

	public Widget asWidget() {
		return this;
	}

	public Widget getDragHandle() {
		return title;
	}

	public IsSheet title() {
		return title;
	}

	public IsSheet preview() {
		return preview;
	}

	public IsSheet footer() {
		return footer;
	}

	public List<Sheet> links() {
		return links;
	}

	public void savePosition() {
		left = title.getAbsoluteLeft();
		top = title.getAbsoluteTop();
	}

	public int leftPosition() {
		return left;
	}

	public int topPosition() {
		return top;
	}

	public void onDragEnd(DragEndEvent event) {
		// Do nothing
	}

	public void onDragStart(DragStartEvent event) {
		if (event.getSource() == title) {
			savePosition();
		}
	}

	public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException {
		// Do nothing
	}

	public void onPreviewDragStart(DragStartEvent event) throws VetoDragException {
		// Do nothing
	}

	public HandlerRegistration addClickHandler(ClickHandler handler) {
		final HandlerRegistration handlerReg = addHandler(handler, ClickEvent.getType());
		handlers.put(handler, handlerReg);
		return handlerReg;
	}

	public void removeClickHandler(ClickHandler handler) {
		final HandlerRegistration handlerReg = handlers.get(handler);
		if (handlerReg != null) {
			handlerReg.removeHandler();
		}
	}

}
